﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SploreClient
{
    public static class Conio
    {
        private const char StartHotKey = '[';
        private const char EndHotKey = ']';
        private const ConsoleColor DefaultBackgroundColor = ConsoleColor.White;
        private const ConsoleColor DefaultForegroundColor = ConsoleColor.Black;
        private const int DefaultWindowWidth = 40;
        private const int DefaultBufferWidth = DefaultWindowWidth;
        private const int DefaultWindowHeight = 30;
        private const int DefaultBufferHeight = DefaultWindowHeight;
        private const ConsoleColor TitleForegroundColor = ConsoleColor.Blue;
        private const ConsoleColor TitleBackgroundColor = DefaultBackgroundColor;
        private const ConsoleColor ContentForegroundColor = ConsoleColor.Black;
        private const ConsoleColor ContentBackgroundColor = DefaultBackgroundColor;
        private const ConsoleColor PromptForegroundColor = ConsoleColor.Magenta;
        private const ConsoleColor PromptBackgroundColor = DefaultBackgroundColor;
        private const ConsoleColor HotKeyForegroundColor = ConsoleColor.Magenta;
        private const ConsoleColor HotKeyBackgroundColor = DefaultBackgroundColor;

        public static void Initialize()
        {
            Console.WindowWidth = DefaultWindowWidth;
            Console.WindowHeight = DefaultWindowHeight;
            Console.BufferWidth = DefaultBufferWidth;
            Console.BufferHeight = DefaultBufferHeight;
            Console.BackgroundColor = DefaultBackgroundColor;
            Console.ForegroundColor = DefaultForegroundColor;
            Console.Clear();
        }
        public static void Write(bool newLineFlag, ConsoleColor theForegroundColor, ConsoleColor theBackgroundColor, string theText, object[] theParams)
        {
            ConsoleColor oldForegroundColor = Console.ForegroundColor;
            ConsoleColor oldBackgroundColor = Console.BackgroundColor;
            Console.ForegroundColor = theForegroundColor;
            Console.BackgroundColor = theBackgroundColor;
            if (theParams != null)
            {
                theText = string.Format(theText, theParams);
            }
            if (newLineFlag)
            {
                Console.WriteLine(theText);
            }
            else
            {
                Console.Write(theText);
            }
            Console.ForegroundColor = oldForegroundColor;
            Console.BackgroundColor = oldBackgroundColor;
        }
        public static void RenderTitleLine(string theTitle)
        {
            RenderTitleLine(theTitle, null);
        }
        public static void RenderTitleLine(string theTitle, params object[] theParams)
        {
            Write(true, TitleForegroundColor, TitleBackgroundColor, theTitle, theParams);
        }
        public static void RenderContentLine(string theContent)
        {
            RenderContentLine(theContent, null);
        }
        public static void RenderContentLine(string theContent, params object[] theParams)
        {
            Write(true, ContentForegroundColor, ContentBackgroundColor, theContent, theParams);
        }
        public static void RenderMenuLine(string theMenuItem)
        {
            RenderMenuLine(theMenuItem,null);
        }
        public static void RenderMenuLine(string theMenuItem, params object[] theParams)
        {
            bool hotkey = false;
            foreach (char character in theMenuItem)
            {
                if (character == StartHotKey)
                {
                    hotkey = true;
                }
                if (hotkey)
                {
                    Write(false, HotKeyForegroundColor, HotKeyBackgroundColor, character.ToString(), null);
                }
                else
                {
                    Write(false, ContentForegroundColor, ContentBackgroundColor, character.ToString(), null);
                }
                if (character == EndHotKey)
                {
                    hotkey = false;
                }
            }
            RenderNewLine();
        }
        public static void RenderPromptLine(string thePrompt)
        {
            RenderPromptLine(thePrompt, null);
        }
        public static void RenderPromptLine(string thePrompt, params object[] theParams)
        {
            Write(true, PromptForegroundColor, PromptBackgroundColor, thePrompt, theParams);
        }
        public static void RenderNewLine()
        {
            Console.WriteLine();
        }
        public static void AnyKeyPromptLine(string thePrompt)
        {
            AnyKeyPromptLine(thePrompt, null);
        }
        public static void AnyKeyPromptLine(string thePrompt, params object[] theParams)
        {
            RenderPromptLine(thePrompt, theParams);
            Console.ReadKey(true);
        }
        public static string ReadFilteredKey(HashSet<string> theFilter)
        {
            string result = string.Empty;
            bool done = false;
            while (!done)
            {
                ConsoleKeyInfo info = Console.ReadKey(true);
                if (theFilter.Contains(info.Key.ToString()))
                {
                    done = true;
                    result = info.Key.ToString();
                }
            }
            return result;
        }
        public static string ReadLine()
        {
            return Console.ReadLine();
        }
    }
}
